//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<FrameComposition.h>
///	@path	~/src/appear/xwin/
///	@date	2008/10/11
///	@desc	.

#pragma once

#include "xeres.h"

#include "graphics/IRenderer.h"
#include "graphics/IRenderTarget.h"
#include "graphics/IRenderTargetSet.h"
#include "graphics/IEffect.h"
#include "graphics/ITechnique.h"
#include "graphics/IRenderState.h"
#include "graphics/IShaderVariable.h"
#include "graphics/IBlendState.h"
#include "graphics/common/common_vertex_decl.h"
#include "graphics/common/TQuadList.h"

namespace xeres {

	// forward declaration
	class IGuiEffect;
	class XFrame;

	/*! 
		\class	FrameCompsition
		\brief	Provide a frame-desktop composition control.
	*/
	class FrameComposition
	{
	public:

		/// \ctor
		FrameComposition( void );

		/// \dtor
		~FrameComposition( void );

	public:

		/// \brief	Initialize frame composition.
		void Initialize( RenderContext& rc , RefWeak<IEffect> effect );

		/// \brief	Finalize frame composition.
		void Finalize( void );

		/// \brief	Prepare for a frame rendering.
		void BeginFrame( RenderContext& rc );

		/// \brief	End frame rendering.
		void EndFrame( RenderContext& rc );

		/// \brief	Resize composition.
		void Resize( RenderContext& rc , const Pair<int>& size );

		/// \brief	Draw frame by texture.
		void DrawFrame( RenderContext& rc , const Rect<int>& placement , RefWeak<ITexture> tex , RefWeak<IGuiEffect> effect );

		/// \brief	Composite frame by aero effect.
		void AeroCompositeFrame( RenderContext& rc , const Rect<int>& placement , RefWeak<ITexture> tex , const Rect<int>& aero );

	private:

		//@data
		Ref<IRenderTarget>		m_desktopRt;
		Ref<IRenderTarget>		m_downSampleRt;

		Ref<IRenderTargetSet>	m_desktopRtSet;
		Ref<IRenderTargetSet>	m_downSampleRtSet;

		// effects

		Ref<IEffect>			m_effect;

		Ref<ITechnique>			m_frameTech;
		Ref<ITechnique>			m_downsampleTech;
		Ref<ITechnique>			m_aeroTech;
		Ref<IShaderVariable>	m_aeroParam;
 		Ref<IShaderVariable>	m_aeroScrSize;
		Ref<IShaderVariable>	m_downsampleOffsetVar;
		Ref<IShaderVariable>	m_frameTextureVar;
		Ref<IShaderVariable>	m_blurTextureVar;

		Ref<IShaderAdapter>		m_frameAdapter;
		Ref<IShaderAdapter>		m_downSampleAdapter;
		Ref<IShaderAdapter>		m_aeroAdapter;

		// render state
		Ref<IRenderState>		m_desktopState;

		// frame geos

		Ref<IVertexLayout>		m_quadLayout;
		TQuadList<vd::ScreenTexture,2>	m_desktopQuad;
		TQuadList<vd::ScreenTexture,2>	m_frameQuad;

		// tags.
		bool					m_enableAero;
		bool					m_spare[3];

		int						m_dx;
		int						m_dy;
		double					m_invertResX;
		double					m_invertResY;
		float					m_invX2;
		float					m_invY2;

		// dirty region.
		std::vector< Rect<int> > m_dirtyRegions;
	};
	
} // namespace xeres
